package cn.bqjr.eily.samples.facade.impl;

import cn.bqjr.eily.samples.entity.po.Role;
import cn.bqjr.eily.samples.entity.po.RoleMenu;
import cn.bqjr.eily.samples.entity.po.UserRole;
import cn.bqjr.eily.samples.entity.vo.RoleVO;
import cn.bqjr.eily.samples.facade.IRoleFacade;
import cn.bqjr.eily.samples.service.base.IMenuService;
import cn.bqjr.eily.samples.service.base.IRoleMenuService;
import cn.bqjr.eily.samples.service.base.IRoleService;
import cn.bqjr.eily.samples.service.base.IUserRoleService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class RoleFacade implements IRoleFacade {

    private final Logger logger = LoggerFactory.getLogger(RoleFacade.class);

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleMenuService roleMenuService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IMenuService menuService;

    @Override
    public boolean addRoleVO(RoleVO roleVO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVO, role);
        roleService.insert(role);

        Long roleId = role.getRoleId();
        List<Long> menuIdList = roleVO.getMenuIdList();
        if (!CollectionUtils.isEmpty(menuIdList)) {
            menuIdList.stream()
                      .filter(menuId -> null != menuService.selectById(menuId))
                      .forEach(menuId -> roleMenuService.insert(
                              new RoleMenu().setRoleId(roleId).setMenuId(menuId)));
        }

        return true;
    }

    @Override
    public boolean updateRoleVO(RoleVO roleVO) {
        Long roleId = roleVO.getRoleId();
        List<Long> menuIdList = roleVO.getMenuIdList();
        if (!CollectionUtils.isEmpty(menuIdList)) {
            roleMenuService.delete(new EntityWrapper<RoleMenu>().eq(RoleMenu.ROLEID, roleId));
            menuIdList.stream()
                      .filter(menuId -> null != menuService.selectById(menuId))
                      .forEach(menuId -> roleMenuService.insert(
                              new RoleMenu().setRoleId(roleId).setMenuId(menuId)));
        }

        roleService.updateById(roleVO);
        return true;
    }

    @Override
    public boolean deleteRole(Long roleId) {
        logger.debug("删除角色表信息...");
        roleService.deleteById(roleId);

        logger.debug("删除角色菜单关联表信息...");
        roleMenuService.delete(new EntityWrapper<RoleMenu>().eq(RoleMenu.ROLEID, roleId));

        logger.debug("删除用户角色关联表信息...");
        userRoleService.delete(new EntityWrapper<UserRole>().eq(UserRole.ROLEID, roleId));

        return true;
    }

    @Override
    public boolean deleteRoleByBatchId(List<Long> roleIds) {
        roleIds.stream().forEach(roleId -> deleteRole(roleId));
        return true;
    }
}
